//
//  XKLMeTableViewController.m
//  GuiZiShouDuanZi
//
//  Created by xmg on 16/4/24.
//  Copyright © 2016年 xiao_kl. All rights reserved.
//

#import "XKLMeTableViewController.h"
#import "XKLSettingTableViewController.h"
#import "XKLMeCollectionViewCell.h"
#import <AFNetworking.h>
#import "XKLMeItem.h"
#import <MJExtension.h>
#import "XKLWebViewController.h"

static CGFloat cols=4;
static CGFloat margin=1;
static NSString *const ID=@"item";
#define wh ((ScreenW-(cols -1)*margin)/cols)

@interface XKLMeTableViewController()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong)UICollectionView *collection;

@property (nonatomic,strong)NSArray *itemArr;

@end


@implementation XKLMeTableViewController


-(void)viewDidLoad{
    [super viewDidLoad];
    
    [self setNavigationBar];
    
    [self setMyTableView];
    
    [self updata];
}

-(void)updata{
    
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    dic[@"a"]=@"square";
    dic[@"c"]=@"topic";
    
    
    [[AFHTTPSessionManager manager] GET:@"http://api.budejie.com/api/api_open.php" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        self.itemArr=[XKLMeItem mj_objectArrayWithKeyValuesArray:responseObject[@"square_list"]];
        
        
        [self addFootView];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        XKLLog(@"%@",error);
    }];
}

-(void)addFootView{
    
    //计算collection高度
    NSInteger row=self.itemArr.count/cols;
    
    if(self.itemArr.count/cols!=0){
        row++;
    }
    //创建一个尾部控制器
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    UICollectionView *collection=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, row * (wh+1)) collectionViewLayout:flowLayout];
    flowLayout.itemSize=CGSizeMake(wh, wh);
    flowLayout.minimumInteritemSpacing=margin;
    flowLayout.minimumLineSpacing=margin;
    
    collection.delegate=self;
    collection.dataSource=self;
    collection.backgroundColor=self.tableView.backgroundColor;
    self.collection=collection;
    
    //设置collection不可以滚动
    self.collection.scrollEnabled=NO;
    
    [collection registerClass:[XKLMeCollectionViewCell class] forCellWithReuseIdentifier:ID];
    self.tableView.tableFooterView=collection;
}


-(void)setMyTableView{
    self.tableView.contentInset=UIEdgeInsetsMake(-25, 0, 0, 0);
    
}

-(void)setNavigationBar{
    
    self.navigationItem.title=@"我的";
    
    UIBarButtonItem *item1=[UIBarButtonItem itemWithImageName:@"mine-setting-icon" seletImageName:@"mine-setting-icon-click" target:self sel:@selector(settingClick:)];
    
    UIBarButtonItem *item2=[UIBarButtonItem itemWithImageName:@"mine-moon-icon" hileghtImageName:@"mine-sun-icon-click" target:self sel:@selector(moonClick:)];
    
    self.navigationItem.rightBarButtonItems=@[item1,item2];
}

-(void)settingClick:(UIButton *)but{
    XKLSettingTableViewController *setting=[[XKLSettingTableViewController alloc] init];
    [self.navigationController pushViewController:setting animated:YES];
    
}

-(void)moonClick:(UIButton *)but{
    but.selected=!but.selected;
}

#pragma mark - 代理
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.itemArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    XKLMeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    XKLMeItem *item=self.itemArr[indexPath.row];
    cell.item=item;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    XKLLogFunc;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    XKLMeItem *item=self.itemArr[indexPath.row];
    
    if (![item.url containsString:@"http"])return;
    
    XKLWebViewController *webVC=[[XKLWebViewController alloc] init];
    webVC.item=item;
    webVC.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:webVC animated:YES];
    
}
@end
